<?php

class Admin_GroupController extends Kieumau_Controller_Kieumauadmin
{
	
	protected 	$fieldsValidate = array('name' => 'isName', 'description' => 'isDescription');
	
    public function init()
    {
		parent::init();
    	try{
    		$dirTemplate = TEMPLATE_PATH;
    		$sectionConfig = 'template';
    		$fileConfig = 'default/template.ini';
    		$this->createTemplate($dirTemplate, $sectionConfig, $fileConfig );
    		
    	}catch(Exception $ex){
    		echo "error: ".$ex->getMessage();
    		die;
    	}
    }
	
	// @author: Peter.Nguyen
	// @data: 5/25/2012
    public function indexAction()
    {
        // action body
		
    }
	
	// @author: Peter.Nguyen
	// @data: 5/18/2012
    public function listAction(){
		
		$groupTable = new Admin_Model_DbTable_Group();
		$resultUser = $groupTable->getTables();
		$page = $this->_getParam ( 'page', 10 );
		$paginator = Zend_Paginator::factory ( $resultUser );
		$paginator->setItemCountPerPage ( 10 );
		$paginator->setCurrentPageNumber ( $page );
		$this->view->languageDefault = $this->language->getLanguageDefault();
		$this->view->paginator = $paginator;

	}
	
	// @author: Peter.Nguyen
	// @data: 5/18/2012
    public function delAction(){
		
		// No need view ...
		$this->_helper->layout->disableLayout ();
     	$this->_helper->viewRenderer->setNoRender ();
		
		$status = array();
		$id = $this->getRequest()->getParam('id');
		$groupTable = new Admin_Model_DbTable_Group();
		$status['status'] = $groupTable->delTable($id);
		echo json_encode($status);
		exit();
		
	}
	
	//
	public function delsAction(){
		// No need view ...
		$this->_helper->layout->disableLayout ();
     	$this->_helper->viewRenderer->setNoRender ();
		
		$status = array();
		$ids = $this->getRequest()->getParam('ids');
		$groupTable = new Admin_Model_DbTable_Group();
		$status['status'] = $groupTable->delTables($ids);
		echo json_encode($status);
		exit();	
	}
	
	// @author: Peter.Nguyen
	// @data: 5/18/2012
    public function addAction(){
	
		// Get data;
		$groupTable = new Admin_Model_DbTable_Group();
		$this->view->languageDefault = $this->language->getLanguageDefault();
		// Process data for input category
		//$groupParent = $user_table->getGroupParentUser();
		//$dataSelectParent = "";
		//$id_group_parent = $this->getRequest()->getPost('group_parent');
		//foreach( $groupParent as $objCat ){
		//	$dataSelectParent[$objCat['id']] = $objCat['name'];			
		//}	
		//$this->view->group_parent = $this->createSelect($dataSelectParent, $id_group_parent);

		//
		$id = $this->getRequest()->getParam('id');
		if($this->getRequest()->isPost()){
			
			$dataForm = $this->getRequest()->getPost();
			$error = $this->fieldsValidate($dataForm);
			$this->view->dataForm = $dataForm;	
			if($dataForm['id'] != "")
				$id = $dataForm['id'];
			
			// If no have error, The system will save data into database		
			if(count($error)==0){
				
				if($id){
					$groupTable->updateTable($dataForm, $id);				
				}else{
					$id = $groupTable->insertTable($dataForm);	
				}
				if($id>0){
					$this->view->successfully = true;	
				}elseif($id=-1){
					$this->view->error = array($this->language->getMessages('en', 'unsuccessfully'));
				}
			}else{
				$this->view->error = $error;
			}
			
		}elseif($id){
			$dataForm = $groupTable->getTable($id);
			if(isset($dataForm))
				$this->view->dataForm = $dataForm;	
		}
		// 
		$this->view->id = $id;

	}
}

